package com.leapp.sk.viewdelegate

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.GlideCircleTransform
import com.example.mvp.view.LKCircleImageView
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.bean.PersonalMineDataEntity

/**
 * 我的页面视图托管
 * @author  chaohaipeng  on 2018/7/4.
 */
class MyDelegate : BaseAppDelegate() {
    /**
     * 图片加载管理
     */
    var mRequestManager: RequestManager? = null

    /**
     * 图片加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()
    /**
     * 图片加载操作
     */
    var mBRequestOptions: RequestOptions = RequestOptions()
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.fragment_my
    }

    fun initData() {
        val rl_fm_head_info = getView<RelativeLayout>(R.id.rl_fm_head_info)
        val layoutParams: LinearLayout.LayoutParams = rl_fm_head_info.layoutParams as LinearLayout.LayoutParams
        layoutParams.width = LKScreenUtil.getScreenWidth()
        layoutParams.height = LKScreenUtil.getScreenWidth() * 3 / 5
        rl_fm_head_info.layoutParams = layoutParams
        val rl_fm_class_index = getView<RelativeLayout>(R.id.rl_fm_class_index)

    }

    /**
     * 设置数据
     */
    fun setData(context: Context,data: PersonalMineDataEntity){

        val tv_fm_parent_name = getView<TextView>(R.id.tv_fm_parent_name)
        setHeadData(data.avatar)
        tv_fm_parent_name.text=data.nickName
        setSex(data.sex)
    }

    /**
     * 设置性别
     */
    fun setSex(sex:Int){
        val tv_fm_parent_mw = getView<ImageView>(R.id.tv_fm_parent_mw)
        if (sex==0){
            tv_fm_parent_mw.setImageResource(R.mipmap.icon_female)
        }else{
            tv_fm_parent_mw.setImageResource(R.mipmap.icon_male)
        }
    }


    /**
     * 设置头像
     */
    fun setHeadData(headUrl: String?) {
        setHeadBackground(headUrl)
        val ima_fm_avatar = getView<LKCircleImageView>(R.id.ima_fm_avatar)

        val tag = ima_fm_avatar.getTag(R.id.tag_head)
        if ( tag==null||!tag?.equals(headUrl)) {
            mRequestManager = Glide.with(getActivity<AppCompatActivity>())
            mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
            mRequestOptions.skipMemoryCache(true)
            mRequestOptions.error(R.mipmap.icon_defout_head)
            mRequestOptions.transforms(GlideCircleTransform(getActivity()))
            mRequestManager?.load(headUrl)
                    ?.thumbnail(0.2f)
                    ?.apply(mRequestOptions)
                    ?.into(ima_fm_avatar)
            ima_fm_avatar.setTag(R.id.tag_head,headUrl)
        }

    }

     fun setHeadBackground(headUrl: String?){
         val iv_fm_head_background = getView<ImageView>(R.id.iv_fm_head_background)
         val tag = iv_fm_head_background.getTag(R.id.tag_head)
         if ( tag==null||!tag?.equals(headUrl)) {
             mRequestManager = Glide.with(getActivity<AppCompatActivity>())
             mBRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
             mBRequestOptions.skipMemoryCache(true)
             mBRequestOptions.error(R.mipmap.icon_techer_detial)
             mRequestManager?.load(headUrl)
                     ?.apply(mBRequestOptions)
                     ?.into(iv_fm_head_background)
             iv_fm_head_background.setTag(R.id.tag_head,headUrl)
         }
     }



}